package com.leo.collisions;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.leo.common.Config;
import com.leo.common.GameType;

import java.util.List;

/**
 * 子弹和土墙的碰撞检测
 */
public class BulletBrickHandler extends CollisionHandler {

    public BulletBrickHandler() {
        super(GameType.BULLET, GameType.BRICK);
    }

    /*@Override
    protected void onCollisionBegin(Entity a, Entity b) {
        FXGL.spawn("explode", a.getPosition());
        a.removeFromWorld();
        b.removeFromWorld();
    }*/

    /**
     * 处理子弹同时碰到多个障碍物时的逻辑
     *
     * @param a
     * @param b
     */
    @Override
    protected void onCollision(Entity a, Entity b) {
        int level = a.getInt("level");
        boolean isRemoveBullet = false;
        List<Entity> entitiesFiltered = FXGL.getGameWorld().getEntitiesFiltered(entity ->
                a.isColliding(entity) && (entity.isType(GameType.STONE)
                        || entity.isType(GameType.GREENS)
                        || entity.isType(GameType.BRICK))
        );
        for (Entity entity : entitiesFiltered) {
            GameType type = (GameType) entity.getType();
            switch (type) {
                case BRICK -> {
                    entity.removeFromWorld();
                    isRemoveBullet = true;
                }
                case STONE -> {
                    if (level >= Config.TANK_MAX_LEVEL) {
                        entity.removeFromWorld();
                    }
                    isRemoveBullet = true;
                }
                case GREENS -> {
                    if (level >= Config.TANK_MAX_LEVEL) {
                        entity.removeFromWorld();
                    }
                }
            }
        }

        if (isRemoveBullet) {
            FXGL.spawn("explode", a.getCenter().subtract(50 / 2.0, 50 / 2.0));
            a.removeFromWorld();
        }
    }
}
